<style scoped>
.menu-container {
    /* background-color: rgb(248 248 248); */
}
.menu-area .el-menu {
    border-right: none;
}
</style>
<!-- background-color="rgb(248 248 248)"
                    text-color="#000000" -->
<template>
    <el-container class="h-full menu-container">
        <el-main class="border-r-1 border-b-1 padding-0 h-0 menu-area">
            <el-scrollbar>
                <el-menu
                    default-active="2"
                    :default-openeds="['1']"
                    @select="onMenuSelect"
                    
                >
                    <el-menu-item index="2">
                        <el-icon>
                            <Menu />
                        </el-icon>
                        <span>所有记录</span>
                    </el-menu-item>
                    <!-- <el-menu-item index="3">
                        <el-icon>
                            <Star />
                        </el-icon>
                        <span>我的收藏</span>
                    </el-menu-item>-->
                    <el-menu-item index="4">
                        <el-icon>
                            <Delete />
                        </el-icon>
                        <span>回收站</span>
                    </el-menu-item>
                    <el-sub-menu index="1">
                        <template #title>
                            <el-icon>
                                <Files />
                            </el-icon>
                            <span>文件夹</span>
                        </template>
                        <el-menu-item
                            v-for="folder in store.folderList"
                            :index="'1-' + folder.id"
                        >{{ folder.name }}</el-menu-item>
                    </el-sub-menu>
                </el-menu>
            </el-scrollbar>
        </el-main>
        <el-footer class="border-r-1 padding-0 footer-opt" height="80px">
            <el-row class="h-full" align="middle">
                <el-col :span="12" class="border-r-1">
                    <div style="text-align: center;">
                        <el-button size="large" type="danger" plain @click="deleteFolder">
                            <el-icon :size="25">
                                <Delete />
                            </el-icon>
                        </el-button>
                    </div>
                </el-col>
                <el-col :span="12">
                    <div style="text-align: center;">
                        <el-button size="large" type="primary" plain @click="showFolderAddWin">
                            <el-icon :size="25">
                                <folder-add />
                            </el-icon>
                        </el-button>
                    </div>
                </el-col>
            </el-row>
        </el-footer>
    </el-container>

    <el-dialog v-model="dialogVisible" title="添加文件夹" width="30%" center>
        <el-form
            ref="newFolderFormRef"
            label-position="right"
            :model="newFolderForm"
            :rules="newFolderFormRules"
            @submit.native.prevent
        >
            <el-form-item label="文件夹名" prop="name">
                <el-input
                    ref="newMenuNameInputRef"
                    v-model="newFolderForm.name"
                    @keyup.enter.native="addFolder(newFolderFormRef)"
                ></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button
                    :loading="addFolderLoading"
                    type="primary"
                    @click="addFolder(newFolderFormRef)"
                >创建</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
//里面的代码会被编译成组件 setup() 函数的内容。
//这意味着与普通的 <script> 只在组件被首次引入的时候执行一次不同，<script setup> 中的代码会在每次组件实例被创建的时候执行。
import {
    Files,
    Menu,
    Delete,
    Star,
    FolderAdd,
} from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { ElForm, ElMenu, ElMessage, ElButton, ElInput } from 'element-plus';
import { useStore } from '../main';

const store = useStore();

/*
仅限 TypeScript 的功能
仅限类型的 props/emit 声明
*/
// const emit = defineEmits<{
//     (e: "selectMenu", index: string): void
//     (e: "folderChange", newFolder: Folder, win: -1 | 1): void
// }>();
// const props = defineProps<{
//     currentDetails: Details
// }>();//定义props中数据类型
// const { currentDetails } = toRefs(props);//对象的每个property都是指向原始对象相应property的ref

store.pwdmApi.folderList().then(folders => {
    store.folderList = folders;
}).catch(error => {
    console.log(error.msg);
});

//选中菜单后的方法
const onMenuSelect = (index: string, indexPath: string[]) => {
    store.selectMenuIndex = index;
};

const deleteFolder = () => {
    if (store.selectMenuIndex.startsWith("1-")) {
        const folderId = Number(store.selectMenuIndex.replace("1-", ""));
        store.pwdmApi.folderDel(folderId).then(() => {
            store.selectMenuIndex = "";
            store.folderList.splice(store.folderList.findIndex(v => v.id == folderId), 1);
            ElMessage({
                message: '文件夹已删除',
                type: 'success',
            });
        }).catch(error => {
            ElMessage({
                message: error.msg,
                type: 'error',
            });
        });
    } else {
        ElMessage({
            message: "请选择文件夹",
            type: 'warning',
        })
    }
};

const newMenuNameInputRef = ref<InstanceType<typeof ElInput>>(); //文件夹名称输入框
const dialogVisible = ref(false);//新建文件夹窗口开关
const showFolderAddWin = () => {
    addFolderLoading.value = false;
    dialogVisible.value = true;
    //设置焦点
    setTimeout(()=> {
        newMenuNameInputRef.value?.focus();
    },500)
};

const newFolderForm = reactive({
    name: '',
});
//表单校验规则
const newFolderFormRules = reactive({
    name: [{
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {
            if (!value) return callback(new Error('请输入文件夹名'));
            if (!value.trim()) {
                callback(new Error('请输入文件夹名'));
            } else {
                callback();
            }
        }
    }]
});

type FormInstance = InstanceType<typeof ElForm>;//创建一个ElForm的类型
const newFolderFormRef = ref<FormInstance>();
const addFolderLoading = ref(false); //按钮加载动画
const addFolder = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    //表单验证
    formEl.validate(valid => {
        if (valid) {
            if (!addFolderLoading.value) {
                addFolderLoading.value = true;
                //验证通过，提交
                store.pwdmApi.folderAdd(newFolderForm.name.trim()).then(newFolder => {
                    store.folderList.push(newFolder);
                    dialogVisible.value = false;
                    newFolderForm.name = '';
                    ElMessage({
                        message: '新文件夹添加成功',
                        type: 'success',
                    });
                }).catch(error => {
                    newFolderForm.name = '';
                    ElMessage({
                        message: error.msg,
                        type: 'error',
                    });
                    addFolderLoading.value = false;
                });
            }
        }
    })
};

</script>